var markerArray = new Array();
var labelArray = new Array();
var markerClusterer;
var mapIconShow = {};
function BaiduMapHandler(_map, mapPath) {
	this.map = _map;
	this.gpsDataMap = {}; //保存当前的实时数据,以GpsRealData表的Id为主键

	this.markerMap = {}; //保存当前的实时marker,以GpsRealData表的Id为主键
	this.areaMap = {}; //保存当前的区域overlay和文本标注,已Enclosure围栏表的id为主键.


	this.mapPath = mapPath;
	this._endIconImageUrl = this.mapPath + "/images/track/end.gif"; //结束地点图标

	this.realDataMap = {};

	//轨迹中回放中的marker, 线等，便于批量清楚
	this.routeOverlays = new Array();
}
/**
 * 将区域对象和对应的文本标注，保存在hash表中，便于调用和删除
 * @enclosureId, 围栏的主键Id
 */
BaiduMapHandler.prototype.cacheAreaOverlay = function (enclosureId, overlay, label) {
	this.areaMap[enclosureId] = {overlay: overlay, label: label};
}
BaiduMapHandler.prototype.getAreaOverlay = function (enclosureId) {
	return this.areaMap[enclosureId];
}

/**
 * 根据车速、方向、报警和上线状态来生成车辆地标
 */
BaiduMapHandler.prototype.getRecordImageUrl = function (direction, velocity, online, alarm) {
	if (!direction) {
		direction = 0;
	}

	if (online == 4) {
		var img = setAngleImage1(direction);
		return this.mapPath + "/images/alarm/" + img;
	}
	if (online == 3) {

		/*	if (typeof (alarm) != "undefined" && parseInt(alarm) != 0) {
		 return this.mapPath + "/images/alarm/" + img;
		 }
		 else*/
		if (velocity <= 1) {
			return this.mapPath + "/images/direction/parking/" + "parking.png";
		}
		else {
			var img = setAngleImage1(direction);
			return this.mapPath + "/images/direction/" + img;
		}
	}

	else {
		return this.mapPath + "/images/direction/off/" + "offline.png";
	}

}


BaiduMapHandler.prototype.getHisRecordImageUrl = function (direction, velocity, online, alarm) {
	if (!direction) {
		direction = 0;
	}

	if (online == 3) {

		if (velocity <= 1) {
			return this.mapPath + "/images/direction/parking/" + "parking.png";
		}
		else {
			return this.mapPath + "/images/direction/" + "0.png";
		}
	}

	else {
		return this.mapPath + "/images/direction/off/" + "offline.png";
	}

}


BaiduMapHandler.prototype.getIconUrlByState = function (state) {
	var iconurl = new String();
	switch (state) {
		case 5:
			iconurl = this.mapPath + "/MapIcon/3-32.png";
			break;
		case 6:
			iconurl = this.mapPath + "/MapIcon/1-32.png";
			break;
		case 7:
			iconurl = this.mapPath + "/MapIcon/4-32.png";
			break;
		case 8:
			iconurl = this.mapPath + "/MapIcon/2-32.png";
			break;
		case 9:
			iconurl = this.mapPath + "/MapIcon/5-32.png";
			break;
		case 10:
			iconurl = this.mapPath + "/MapIcon/6-32.png";
			break;
		case 11:
			iconurl = this.mapPath + "/MapIcon/7-32.png";
			break;
		case 12:
			iconurl = this.mapPath + "/MapIcon/9-32.png";
			break;
		case 13:
			iconurl = this.mapPath + "/MapIcon/10-32.png";
			break;
		default:
			iconurl = this.mapPath + "/MapIcon/8-32.png";
	}
	return iconurl;

}


BaiduMapHandler.prototype.getAlarmIconUrlByAlarmType = function (AlarmTypeForIcon) {
	var iconurl = new String();
	switch (AlarmTypeForIcon) {
		case 'BDSOS':
			iconurl = this.mapPath + "/MapIcon/10-32.png";
			break;
		default:
			iconurl = this.mapPath + "/MapIcon/4-32.png";
	}
	return iconurl;

}


//**************************************************************带数据表格的轨迹绘制
BaiduMapHandler.prototype.drawRoute = function (record) {
	record = convertRecord(record);
	var courseLat = parseFloat(record.latitude);
	var courseLng = parseFloat(record.longitude);
	if (this.startMarker == null) {
		var iconPath = this.mapPath + "/images/track/start.gif";
		var icon = this.createIcon(iconPath, 39, 25);
		this.startMarker = this.addHisGpsMarker(record.longitude, record.latitude,
			icon, record, false);
		this.routeOverlays.push(this.startMarker);

		this._preLat = record.latitude;
		this._preLng = record.longitude;
		this._preRecord = record;
	} else {
		// 这一次循环的时间
		var time = record.sendTime;

		var imageUrl = this.getHisRecordImageUrl(record.direction, record.velocity, 3, record.alarm);

		var currentPos = new BMap.Point(record.longitude, record.latitude);

		if (this.courseMarker == null) {
			var icon = this.createIcon(imageUrl, 32);
			this.courseMarker = this.addHisGpsMarker(record.longitude, record.latitude,
				icon, record, false);

			this.routeOverlays.push(this.courseMarker);
			this.setCenter(record.latitude, record.longitude);
			if (record.velocity > 0) {
				var icon1 = this.createIcon(this.mapPath + "/images/direction/" + "0.png", 32);
				this.courseMarker.setIcon(icon1);
				this.courseMarker.setRotation(record.direction);
			}

		} else {
			this.updateMarker(this.courseMarker,record);
			this.courseMarker.setPosition(currentPos);

			if (record.velocity > 0) {
				var icon1 = this.createIcon(this.mapPath + "/images/direction/" + "0.png", 32);
				this.courseMarker.setIcon(icon1);
				this.courseMarker.setRotation(record.direction);
			}
		}
		if (this.hisCarLabel) {
			this.map.removeOverlay(this.hisCarLabel);
		}
		var descr = record.equipName + "时间:" + record.sendTime;
		this.hisCarLabel = this.createMarkerLabel(record.simNo, record.latitude, record.longitude, descr, 32);
		if (this.isInMapBound(currentPos) == false) {
			this.setCenter(record.latitude, record.longitude);
		}
		// 绘制前一点的图标
		var currentMarker;
		var prePos = new BMap.Point(this._preLng, this._preLat);
		var distance = this.map.getDistance(prePos, currentPos);
		if (distance > 20) {
			var tempImageUrl = this.mapPath + "/images/toolbar/point.png";
			var icon = this.createIcon(tempImageUrl);
			var dvsDirectionMarker = this.addHisGpsMarker(this._preLng, this._preLat,
				icon, this._preRecord, false);
			//压入到轨迹层，便于一次性清除轨迹
			this.routeOverlays.push(dvsDirectionMarker);
			if (distance < 100000) {
				//画线
				var line = this.createPolyline([prePos,
					currentPos]);
			}
			this._preLat = record.latitude;
			this._preLng = record.longitude;
			this._preRecord = record;
			//压入到轨迹层，便于一次性清除轨迹
			this.routeOverlays.push(line);
		}

		//if (this._nCount > 1) {
		//}


	}
}


BaiduMapHandler.prototype.drawRouteMonitoringAlone = function (record) {
	record = convertRecord(record);
	var courseLat = parseFloat(record.latitude);
	var courseLng = parseFloat(record.longitude);

	if (this.startMarker == null) {
		var iconPath = this.mapPath + "/images/track/start.gif";
		var icon = this.createIcon(iconPath, 39, 25);
		this.startMarker = this.addMarker(record.longitude, record.latitude,
			icon, record, false);
		this.routeOverlays.push(this.startMarker);

		this._preLat = record.latitude;
		this._preLng = record.longitude;
		this._preRecord = record;
	} else {
		// 这一次循环的时间
		var time = record.sendTime;

		var imageUrl = this.getRecordImageUrl(record.direction, record.velocity, true, record.alarm);

		var currentPos = new BMap.Point(record.longitude, record.latitude);

		if (this.courseMarker == null) {
			var icon = this.createIcon(imageUrl, 32);
			this.courseMarker = this.addMarker(record.longitude, record.latitude,
				icon, record, false);

			this.routeOverlays.push(this.courseMarker);
			this.setCenter(record.latitude, record.longitude);
			this.courseMarker.setRotation(record.direction);
		} else {
			this.courseMarker.setPosition(currentPos);
			this.courseMarker.setRotation(record.direction);

		}
		if (this.hisCarLabel) {
			this.map.removeOverlay(this.hisCarLabel);
		}
		var descr = record.simNo + ",车速:" + record.velocity + "km/h,时间:" + record.sendTime;
		this.hisCarLabel = this.createMarkerLabel(record.simNo, record.latitude, record.longitude, descr, 32);
		if (this.isInMapBound(currentPos) == false) {
			this.setCenter(record.latitude, record.longitude);
		}

		// 绘制前一点的图标
		var currentMarker;

		var prePos = new BMap.Point(this._preLng, this._preLat);
		var distance = this.map.getDistance(prePos, currentPos);
		if (distance > 20) {

			var tempImageUrl = this.mapPath + "/images/toolbar/point.png";
			var icon = this.createIcon(tempImageUrl);
			var dvsDirectionMarker = this.addMarker(record.longitude, record.latitude,
				icon, record, false);
			//压入到轨迹层，便于一次性清除轨迹
			this.routeOverlays.push(dvsDirectionMarker);
			//画线
			var line = this.createPolyline([prePos,
				currentPos]);
			this._preLat = record.latitude;
			this._preLng = record.longitude;
			this._preRecord = record;
			//压入到轨迹层，便于一次性清除轨迹
			this.routeOverlays.push(line);
		}


	}
}


/**
 * 停止轨迹播放
 */
BaiduMapHandler.prototype.drawStop = function () {
	if (this.courseMarker == null) {
		return;
	}
	/*	var icon = this.createIcon(this._endIconImageUrl, 39, 25);
	 this.endMarker = this.addHisGpsMarker(this.courseMarker.getPosition().lng, this.courseMarker.getPosition().lat, icon);
	 this.routeOverlays.push(this.endMarker);*/
	//this.map.removeOverlay(this.courseMarker);
	this.startMarker = null;
	this.courseMarker = null;
}

/**
 * 清楚所有的元素
 */
BaiduMapHandler.prototype.clearAllElement = function () {
	if (this.hisCarLabel) {
		this.map.removeOverlay(this.hisCarLabel);
	}
	this.startMarker = null;
	this.courseMarker = null;

	//清楚轨迹中的地标
	for (var m = 0; m < this.routeOverlays.length; m++) {
		var overlay = this.routeOverlays[m];
		this.map.removeOverlay(overlay); //清除地图marker
	}
	routeOverlays = []; //清空数组
}


/**
 * 清楚所有的车辆元素
 */
BaiduMapHandler.prototype.clearAllVehicle = function () {
	for (var m in this.realDataMap) {
		var rd = this.realDataMap[m];
		this.map.removeOverlay(rd.marker);
		this.map.removeOverlay(rd.label);
	}
	this.realDataMap = {};

}

/**
 * 根据id删除电子围栏
 */
BaiduMapHandler.prototype.removeMapArea = function (enclosureId) {
	var area = this.areaMap[enclosureId];
	if (area) {
		this.map.removeOverlay(area.overlay);
		this.map.removeOverlay(area.label);
	} else {
		//alert("该区域不存在，无法删除");
	}
}

BaiduMapHandler.prototype.setCenter = function (lat, lng, zoom) {
	if (!zoom)
		zoom = this.map.getZoom();
	this.map.centerAndZoom(new BMap.Point(lng, lat), 19);
}


/**
 * 批量在地图上显示gps车辆地标
 * @gpsDataArray页面传送的gps数据，可以同时显示多个地标，所以是数组
 * @isOpenInfoWindow 是否打开信息窗口
 */
BaiduMapHandler.prototype.showMarkers = function (gpsDataArray, isOpenInfoWindow) {
	//for (var i = 0; i < gpsDataArray.length; i++) {
	var gpsData = gpsDataArray[0];
	if (gpsData.longitude > 1 && gpsData.latitude > 1) {
		//continue;

		//var currentPos = new BMap.Point(gpsData.dvsLongitude, gpsData.dvsLatitude);
		//在地图边界外的不进行刷新
		//if(this.isInMapBound(currentPos) == false)
		// continue;
		//var oldRealDataMarker = this.realDataMap[gpsData.dvcSim];
		//var descr = gpsData.dvcSim + "[" + gpsData.depName + "]";
		/**if (gpsData.dvsOnline) {
		if (gpsData.dvsSpeed == 0)
		descr += "停车中";
		else
		descr += "车速:" + gpsData.dvsSpeed + "km/h";
		} else
		 descr += "离线中";**/


		var iconPath = this.getRecordImageUrl(gpsData.direction, gpsData.velocity, gpsData.online, gpsData.alarmState);
		var icon = this.createIcon(iconPath, 32);
		// isOpenInfoWindow = false;
		var marker = this.addRealDataMarker(gpsData.longitude, gpsData.latitude, icon, gpsData, isOpenInfoWindow);

		//labelArray[gpsData.dvcSim] = label;
		//if(isCenter)
		//this.setCenter(gpsData.dvsLatitude, gpsData.dvsLongitude);

	}
	//this.addGoogleMarker(marker);
	//}
}

BaiduMapHandler.prototype.showMarkers_1 = function (gpsDataArray, isOpenInfoWindow) {
	var markers = [];
	for (var i = 0; i < gpsDataArray.length; i++) {
		var gpsData = gpsDataArray[i];
		if (gpsData.latitude > 0 && gpsData.longitude > 0) {

			var currentPos = new BMap.Point(gpsData.longitude, gpsData.latitude);
			var oldRealDataMarker = this.realDataMap[gpsData.simNo];
			var marker = null;
			var descr = gpsData.equipName + ",速度:" + gpsData.velocity + "km/h";
			var label = this.createMarkerLabel1(gpsData.simNo, gpsData.longitude, gpsData.latitude, descr, 32);
			if (oldRealDataMarker != null) {

				//this.map.removeOverlay(oldRealDataMarker.marker);
				//this.map.removeOverlay(oldRealDataMarker.label);
				oldRealDataMarker.marker.setPosition(currentPos);
				var iconPath = this.getRecordImageUrl(gpsData.direction, gpsData.velocity,
					gpsData.online, gpsData.alarmState);
				var icon = this.createIcon(iconPath, 32);
				oldRealDataMarker.marker.setIcon(icon);
				//oldRealDataMarker.marker.getIcon().setImageUrl(iconPath);
				//oldRealDataMarker.label.setPosition(currentPos);

				/*	if (gpsData.velocity > 1) {
				 //console.info(oldRealDataMarker.marker);
				 oldRealDataMarker.marker.setRotation(gpsData.direction);
				 } else {
				 oldRealDataMarker.marker.setRotation(0);
				 }*/

				oldRealDataMarker.marker.getLabel().setContent(descr);
				oldRealDataMarker.gpsData = gpsData;
				oldRealDataMarker.marker.closeInfoWindow();
				//markers.push(oldRealDataMarker.marker);
			} else {
				if (markerClusterer != null) {
					markerClusterer.clearMarkers();
				}
				var iconPath = this.getRecordImageUrl(gpsData.direction, gpsData.velocity,
					gpsData.online, gpsData.alarmState);
				var icon = this.createIcon(iconPath, 32);
				// isOpenInfoWindow = false;
				var marker = this.addRealDataMarker_1(gpsData.longitude, gpsData.latitude, icon, gpsData, isOpenInfoWindow);
				/*	if (gpsData.velocity > 1) {
				 marker.setRotation(gpsData.direction);
				 } else {
				 marker.setRotation(0);
				 }*/
				this.realDataMap[gpsData.simNo] = {marker: marker, gpsData: gpsData};
				mapIconShow[gpsData.simNo] = {marker: marker, gpsData: gpsData};
				//markerArray[gpsData.dvcSim] = marker;
				marker.setLabel(label);
				markers.push(marker);
				//if(isCenter)
				//this.setCenter(gpsData.latitude, gpsData.longitude);
			}
		}
		//this.addGoogleMarker(marker);
	}

	if (markers.length > 0) {
		markerClusterer = new BMapLib.MarkerClusterer(this.map, {markers: markers});
	}


	//console.info(this.realDataMap.length);


	/**for (var i = 0; i < gpsDataArray.length; i++) {
	var gpsData = gpsDataArray[i];
	if (gpsData.dvsLatitude > 0 && gpsData.dvsLongitude > 0) {

	var iconPath = this.getIconUrlByState(gpsData.dvsState);
	var icon = this.createIcon(iconPath, 32);
	// isOpenInfoWindow = false;
	var marker =  this.addRealDataMarker_1(gpsData.dvsLongitude, gpsData.dvsLatitude, icon, gpsData, isOpenInfoWindow);
	markers.push(marker);
	}

	//pt = new BMap.Point(Math.random() * 40 + 85, Math.random() * 30 + 21);
		
	}
	 //最简单的用法，生成一个marker数组，然后调用markerClusterer类即可。
	 var markerClusterer = new BMapLib.MarkerClusterer(this.map, { markers: markers });**/
}


/**
 * 点击分组清除地图车辆图标
 */
BaiduMapHandler.prototype.clearMarkers = function (gpsDataArray, isOpenInfoWindow) {
	var markers = [];
	for (var i = 0; i < gpsDataArray.length; i++) {
		var gpsData = gpsDataArray[i];
		var rd = mapIconShow[gpsData.simNo];
		if (typeof (rd) == "undefined")
			continue;
		markers.push(rd.marker);
		//this.map.removeOverlay(rd.marker);
		delete this.realDataMap[gpsData.simNo];
	}

	markerClusterer.removeMarkers(markers);
}


/**
 * 点击设备清除地图车辆图标
 */
BaiduMapHandler.prototype.clearMarkerDevice = function (sim) {
	var rd = mapIconShow[sim];
	markerClusterer.removeMarker(rd.marker);
	delete this.realDataMap[sim];
}


/**
 * 点击设备清除地图车辆图标
 */
BaiduMapHandler.prototype.clearMarkerDeviceSearch = function (sims) {
	var str = new Array();
	str = sims.split(",");
	var markers = [];
	for (i = 0; i < str.length; i++) {
		var sim = str[i];
		var rd = mapIconShow[sim];
		if (typeof (rd) == "undefined")
			continue;
		markers.push(rd.marker);
		delete this.realDataMap[sim];
	}
	markerClusterer.removeMarkers(markers);

}

/**
 * 批量在地图上显示gps车辆地标
 * @gpsDataArray页面传送的gps数据，可以同时显示多个地标，所以是数组
 * @isOpenInfoWindow 是否打开信息窗口
 */
BaiduMapHandler.prototype.centerMarkerOverlays = function (gpsDataArray, isOpenInfoWindow, isCenter) {
	for (var i = 0; i < gpsDataArray.length; i++) {
		var gpsData = gpsDataArray[i];
		if (gpsData.longitude < 1 || gpsData.latitude < 1 || gpsData.latitude == null)
			continue;
		var oldRealDataMarker = this.realDataMap[gpsData.simNo];
		//var descr = gpsData.dvsSim + "[" + gpsData.depName + "]";
		/**if (gpsData.dvsOnline) {
		if (gpsData.dvsSpeed == 0)
		descr += "停车中";
		else
		descr += "车速:" + gpsData.dvsSpeed + "km/h";
		} else
		 descr += "离线中";**/
		if (oldRealDataMarker != null) {
			var currentPos = new BMap.Point(gpsData.longitude, gpsData.latitude);
			oldRealDataMarker.marker.setPosition(currentPos);
			var iconPath = this.getRecordImageUrl(gpsData.direction, gpsData.velocity, gpsData.online, gpsData.alarmState);
			//oldRealDataMarker.marker.getIcon().setImageUrl(iconPath);
			var icon = this.createIcon(iconPath, 32);
			oldRealDataMarker.marker.setIcon(icon);
			//oldRealDataMarker.label.setPosition(currentPos);
			/*	if (gpsData.velocity > 1) {
			 oldRealDataMarker.marker.setRotation(gpsData.direction);
			 } else {
			 oldRealDataMarker.marker.setRotation(0);
			 }*/
			//oldRealDataMarker.label.setContent(descr);
			oldRealDataMarker.gpsData = gpsData;
			var htmlContent = window.parent.renderTemplate(gpsData);
			var markerInfoWindow = new BMap.InfoWindow(htmlContent);
			oldRealDataMarker.marker.openInfoWindow(markerInfoWindow);
		} else if (!isNaN(gpsData.latitude)) {

			var iconPath = this.getRecordImageUrl(gpsData.direction, gpsData.velocity, gpsData.online, gpsData.alarmState);
			var icon = this.createIcon(iconPath, 32);
			// isOpenInfoWindow = false;
			//alert(gpsData.dvsLongitude);
			var marker = this.addRealDataMarker(gpsData.longitude, gpsData.latitude, icon, gpsData, isOpenInfoWindow);
			/*	if (gpsData.velocity > 1) {
			 marker.setRotation(gpsData.direction);
			 } else {
			 marker.setRotation(0);
			 }*/
			//var label = this.createMarkerLabel(gpsData.dvcSim, gpsData.dvsLatitude, gpsData.dvsLongitude, descr, 32);
			this.realDataMap[gpsData.simNo] = {marker: marker, gpsData: gpsData};
		}
		if (!isNaN(gpsData.latitude))
			if (isCenter)
				this.setCenter(gpsData.latitude, gpsData.longitude);
		//this.addGoogleMarker(marker);
	}
}


/**
 * 点击报警数据，展示在地图
 */
BaiduMapHandler.prototype.centerAlarmMarkerOverlays = function (alarm, isOpenInfoWindow, isCenter) {
	var oldRealDataMarker = this.realDataMap[alarm.simNo];
	if (oldRealDataMarker != null) {
		var currentPos = new BMap.Point(alarm.longitude, alarm.latitude);
		oldRealDataMarker.marker.setPosition(currentPos);
		var iconPath = this.getAlarmIconUrlByAlarmType(alarm.AlarmTypeForIcon);
		var icon = this.createIcon(iconPath, 32);
		oldRealDataMarker.marker.setIcon(icon);
		oldRealDataMarker.gpsData = alarm;
		var htmlContent = window.parent.renderTemplateAlarm(alarm);
		var markerInfoWindow = new BMap.InfoWindow(htmlContent);
		oldRealDataMarker.marker.openInfoWindow(markerInfoWindow);
	} else {

		var iconPath = this.getAlarmIconUrlByAlarmType(alarm.AlarmTypeForIcon);
		var icon = this.createIcon(iconPath, 32);
		var marker = this.addRealDataAlarmMarker(alarm.longitude, alarm.latitude, icon, alarm, isOpenInfoWindow);
		this.realDataMap[alarm.simNo] = {marker: marker, gpsData: alarm};

	}
	if (isCenter)
		this.setCenter(alarm.latitude, alarm.longitude);

}


BaiduMapHandler.prototype.planDriveRoute = function (startPOI, endPOI) {
	var driving = new BMap.DrivingRoute(this.map, {
		renderOptions: {
			map: this.map,
			panel: "r-result",
			autoViewport: true
		}
	});
	driving.search(startPOI, endPOI);

}

BaiduMapHandler.prototype.createLocalSearch = function (onSearchResult) {
	var me = this;
	var options = {
		onSearchComplete: function (results) {
			// 判断状态是否正确
			if (search.getStatus() == BMAP_STATUS_SUCCESS) {
				var poiList = [];
				for (var i = 0; i < results.getCurrentNumPois(); i++) {
					var poi = results.getPoi(i);
					var name = poi.title + ", " + results.getPoi(i).address;
					var code = i;
					poiList.push({result: name, value: poi.address, data: poi});
				}
				onSearchResult(me.keyword, poiList);
			}
		}
	};
	var search = new BMap.LocalSearch('泰州市', options);
	return search;
}

BaiduMapHandler.prototype.createVehicleMarker = function (bmPoint, bIcon, opt, isOpenInfoWindow) {
	var marker = new BMap.Marker(bmPoint, {icon: bIcon});  // 创建标注
	map.addOverlay(marker);              // 将标注添加到地图中
	var htmlContent = window.parent.renderTemplate(opt);
	var markerInfoWindow = new BMap.InfoWindow(htmlContent);
	marker.addEventListener("click", function () {
		this.openInfoWindow(markerInfoWindow);
	});
	if (isOpenInfoWindow) {
		marker.openInfoWindow(markerInfoWindow);
	}
	return marker;
}


BaiduMapHandler.prototype.createIcon = function (iconPath, width, height) {
	//默认是16*16
	if (!width)
		width = 16;
	if (!height)
		height = width;
	return new BMap.Icon(iconPath, new BMap.Size(width, height));
}

BaiduMapHandler.prototype.openInfoWindow = function (dvcSim) {
	var realData = this.realDataMap[dvcSim];
	if (realData) {
		var opt = realData.gpsData;
		var htmlContent = window.parent.renderTemplate(opt);
		var markerInfoWindow = new BMap.InfoWindow(htmlContent);
		realData.marker.openInfoWindow(markerInfoWindow);
	}
}

BaiduMapHandler.prototype.openInfoWindowAlarm = function (dvcSim) {
	var realData = this.realDataMap[dvcSim];
	if (realData) {
		var opt = realData.gpsData;
		var htmlContent = window.parent.renderTemplateAlarm(opt);
		var markerInfoWindow = new BMap.InfoWindow(htmlContent);
		realData.marker.openInfoWindow(markerInfoWindow);
	}
}

BaiduMapHandler.prototype.openHtmlWindow = function (marker, htmlContent) {
	var markerInfoWindow = new BMap.InfoWindow(htmlContent);
	marker.openInfoWindow(markerInfoWindow);
}


BaiduMapHandler.prototype.addMarker = function (lng, lat, icon, opt, isOpenInfoWindow) {
	var pt = new BMap.Point(lng, lat);
	//var myIcon = new BMap.Icon(iconPath, new BMap.Size(16,16));
	var marker = new BMap.Marker(pt, {icon: icon});  // 创建标注
	this.map.addOverlay(marker);              // 将标注添加到地图中
	if (opt) {
		var dvcId = opt.id;
		var me = this;
		var htmlContent = window.parent.renderTemplate(opt);
		marker.addEventListener("click", function () {
			//me.openInfoWindow(dvcId);
			//me.openHtmlWindow(marker,htmlContent);
			var markerInfoWindow = new BMap.InfoWindow(htmlContent);
			marker.openInfoWindow(markerInfoWindow);
		});
		if (isOpenInfoWindow) {
			var markerInfoWindow = new BMap.InfoWindow(htmlContent);
			marker.openInfoWindow(markerInfoWindow);
		}
	}
	return marker;
}

BaiduMapHandler.prototype.addHisGpsMarker = function (lng, lat, icon, opt, isOpenInfoWindow) {
	var pt = new BMap.Point(lng, lat);
	//var myIcon = new BMap.Icon(iconPath, new BMap.Size(16,16));
	var marker = new BMap.Marker(pt, {icon: icon});  // 创建标注
	this.map.addOverlay(marker);              // 将标注添加到地图中
	if (opt) {
		var dvcId = opt.id;
		var me = this;
		var htmlContent = window.parent.renderTemplateHisGps(opt);
		marker.addEventListener("click", function () {
			//me.openInfoWindow(dvcId);
			//me.openHtmlWindow(marker,htmlContent);
			var markerInfoWindow = new BMap.InfoWindow(htmlContent);
			marker.openInfoWindow(markerInfoWindow);
		});
		if (isOpenInfoWindow) {
			var markerInfoWindow = new BMap.InfoWindow(htmlContent);
			marker.openInfoWindow(markerInfoWindow);
		}
	}
	return marker;
}


BaiduMapHandler.prototype.updateMarker = function (marker, opt) {
	if (opt) {
		var dvcId = opt.id;
		var me = this;
		var htmlContent = window.parent.renderTemplateHisGps(opt);
		marker.addEventListener("click", function () {
			//me.openInfoWindow(dvcId);
			//me.openHtmlWindow(marker,htmlContent);
			var markerInfoWindow = new BMap.InfoWindow(htmlContent);
			marker.openInfoWindow(markerInfoWindow);
		});
	}
	return marker;
}


BaiduMapHandler.prototype.addRealDataAlarmMarker = function (lng, lat, icon, opt, isOpenInfoWindow) {
	var pt = new BMap.Point(lng, lat);
	//var myIcon = new BMap.Icon(iconPath, new BMap.Size(16,16));
	var marker = new BMap.Marker(pt, {icon: icon});  // 创建标注
	this.map.addOverlay(marker);              // 将标注添加到地图中
	if (opt) {
		var dvcSim = opt.simNo;
		var me = this;
		marker.addEventListener("click", function () {
			me.openInfoWindowAlarm(dvcSim);
		});
		if (isOpenInfoWindow) {
			var htmlContent = window.parent.renderTemplateAlarm(opt);
			var markerInfoWindow = new BMap.InfoWindow(htmlContent);
			marker.openInfoWindow(markerInfoWindow);
		}
	}
	return marker;
}

BaiduMapHandler.prototype.addRealDataMarker = function (lng, lat, icon, opt, isOpenInfoWindow) {
	var pt = new BMap.Point(lng, lat);
	//var myIcon = new BMap.Icon(iconPath, new BMap.Size(16,16));
	var marker = new BMap.Marker(pt, {icon: icon});  // 创建标注
	this.map.addOverlay(marker);              // 将标注添加到地图中
	if (opt) {
		var dvcSim = opt.simNo;
		var me = this;
		marker.addEventListener("click", function () {
			me.openInfoWindow(dvcSim);
		});
		if (isOpenInfoWindow) {
			var htmlContent = window.parent.renderTemplate(opt);
			var markerInfoWindow = new BMap.InfoWindow(htmlContent);
			marker.openInfoWindow(markerInfoWindow);
		}
	}
	return marker;
}

BaiduMapHandler.prototype.addRealDataMarker_1 = function (lng, lat, icon, opt, isOpenInfoWindow) {
	var pt = new BMap.Point(lng, lat);
	//var myIcon = new BMap.Icon(iconPath, new BMap.Size(16,16));
	var marker = new BMap.Marker(pt, {icon: icon});  // 创建标注
	//this.map.addOverlay(marker);              // 将标注添加到地图中
	if (opt) {
		var dvcSim = opt.simNo;
		var me = this;
		marker.addEventListener("click", function () {
			me.openInfoWindow(dvcSim);
		});
		if (isOpenInfoWindow) {
			var htmlContent = window.parent.renderTemplate(opt);
			var markerInfoWindow = new BMap.InfoWindow(htmlContent);
			marker.openInfoWindow(markerInfoWindow);
		}
	}
	return marker;
}


BaiduMapHandler.prototype.createMarker = function (lng, lat, iconPath, opt, isOpenInfoWindow) {
	var pt = new BMap.Point(lng, lat);
	var myIcon = this.createIcon(iconPath);
	var marker = new BMap.Marker(pt, {icon: myIcon});  // 创建标注
	//map.addOverlay(marker);              // 将标注添加到地图中
	var htmlContent = window.parent.renderTemplate(opt);
	var markerInfoWindow = new BMap.InfoWindow(htmlContent);
	marker.addEventListener("click", function () {
		this.openInfoWindow(markerInfoWindow);
	});
	if (isOpenInfoWindow) {
		marker.openInfoWindow(markerInfoWindow);
	}
	return marker;
}
BaiduMapHandler.prototype.createPolyline = function (points) {
	var polyline = new BMap.Polyline(points,
		{strokeColor: "blue", strokeWeight: 2, strokeOpacity: 0.5});
	this.map.addOverlay(polyline);
	return polyline;
}
/**
 * 判断点是否在地图边界中
 */
BaiduMapHandler.prototype.isInMapBound = function (bmPoint) {
	var bounds = this.map.getBounds();
	return bounds.containsPoint(bmPoint);
}
/**
 * 创建标注
 */
BaiduMapHandler.prototype.createLabel = function (id, lat, lng, text, offsetY) {
	if (!offsetY)
		offsetY = -10;
	var offsetX = -8 * text.length / 2;
	//alert(offsetX);
	var point = new BMap.Point(lng, lat);
	var opts = {
		position: point,    // 指定文本标注所在的地理位置
		offset: new BMap.Size(offsetX, 26)    //设置文本偏移量
	}
	var label = new BMap.Label(text, opts);  // 创建文本标注对象
	label.setStyle({
		color: "blue",
		fontSize: "12px",
		height: "20px",
		border: 0,
		background: "transparent",
		lineHeight: "20px",
		fontFamily: "微软雅黑"
	});
	map.addOverlay(label);
	return label;
}

BaiduMapHandler.prototype.createMarkerLabel = function (id, lat, lng, text, offsetY) {
	if (!offsetY)
		offsetY = -10;
	var offsetX = -8 * text.length / 2;
	//alert(offsetX);
	var point = new BMap.Point(lng, lat);
	var opts = {
		position: point,    // 指定文本标注所在的地理位置
		offset: new BMap.Size(offsetX, 32)    //设置文本偏移量
	}
	var label = new BMap.Label(text, opts);  // 创建文本标注对象
	label.setStyle({
		color: "blue",
		fontSize: "12px",
		height: "20px",
		border: 0,
		background: "#CCFFCC",
		lineHeight: "20px",
		fontFamily: "微软雅黑"
	});
	map.addOverlay(label);
	return label;
}

BaiduMapHandler.prototype.createMarkerLabel1 = function (id, lat, lng, text, offsetY) {
	if (!offsetY)
		offsetY = -10;
	var offsetX = -8 * text.length / 2;
	//alert(offsetX);
	var point = new BMap.Point(lng, lat);
	var opts = {
		position: point,    // 指定文本标注所在的地理位置
		offset: new BMap.Size(offsetX, 32)    //设置文本偏移量
	}
	var label = new BMap.Label(text, opts);  // 创建文本标注对象
	label.setStyle({
		color: "blue",
		fontSize: "12px",
		height: "20px",
		border: 0,
		background: "#CCFFCC",
		lineHeight: "20px",
		fontFamily: "微软雅黑"
	});
	//map.addOverlay(label);
	return label;
}

/**
 *单独监控的时候，不断的刷新最新的位置图标
 */
BaiduMapHandler.prototype.createMarkerOverlayTrackMode = function (targetInfo) {
	//try {
	var currentPos = new BMap.Point(targetInfo.rLng, targetInfo.rLat);
	//过滤无效的日期时间
	//if (parseInt(targetInfo.gpsTime.split(" ")[0].split("-")[0]) < this._defaultYearFilter)
	//return;
	var operatorObj = this;
	// 创建标记
	var vehicledvsDirectionImagUrl = this.mapPath + "/images/dvsDirection/"
		+ setAngleImage(targetInfo.angleInt);
	//var currentGpsTimeMill = parseDate(targetInfo.gpsTime).getTime();

	// 绑定标记
	// this._handler.bindClickEvent(targetInfo.marker, targetInfo, this.bindClickEventHTML);
	/**
	 this._handler.bindClickEvent(targetInfo.marker, targetInfo, function(opts){
	return that.bindClickEventHTML(opts);
	},function() {
	operatorObj._handler.getAddress(targetInfo.id, targetInfo.rLat, targetInfo.rLng);
	});
	 */
	var gpsDataId = targetInfo.plateNo;
	var marker = this.markerMap[gpsDataId];
	var gpsData = this.gpsDataMap[gpsDataId];
	if (!marker) {
		var vehicleIcon = new BMap.Icon(vehicledvsDirectionImagUrl, new BMap.Size(32, 32));
		marker = this.createVehicleMarker(currentPos,
			vehicleIcon, targetInfo, false);

		this.markerMap[gpsDataId] = marker;
		this.setCenter(targetInfo.rLat, targetInfo.rLng, 16);
		this.markerMap[gpsDataId] = marker;
	} else {
		marker.setPosition(currentPos); //更新车辆的当前位置
		var dvsDirectionImagUrl = this.mapPath + "/images/toolbar/point.png";
		var icon = this.createIcon(dvsDirectionImagUrl);
		var dvsDirectionMarker = this.addMarker(gpsData.rLng, gpsData.rLat,
			icon, gpsData, false);
		var prePos = new BMap.Point(gpsData.rLng, gpsData.rLat);
		var distance = this.map.getDistance(prePos, currentPos);
		var points = new Array();
		points[0] = prePos;
		points[1] = currentPos;
		this.createPolyline(points);
		/**
		 if(distance < 20)
		 {
		 operatorObj._handler
		 .removeOverlay(preTargetInfo.marker); //清楚掉前面的marker,避免密集显示图标
		 //return;
		 }else
		 {
		 preTargetInfo.marker.setIconImage(this._currentImageUrl);
		 }*/
	}
	if (this.isInMapBound(currentPos) == false) {
		this.setCenter(targetInfo.rLat, targetInfo.rLng);
	}
	this.gpsDataMap[gpsDataId] = targetInfo;
	//} catch (e) {

	//}
}
